<?php
/**
 * tpAdmin [a web admin based ThinkPHP5]
 *
 * @author yuan1994 <tianpian0805@gmail.com>
 * @link http://tpadmin.yuan1994.com/
 *
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

//------------------------
// 用户控制器
//-------------------------

namespace app\admin\controller\qzuser;

use app\common\controller\Backend;
use think\Db;

//use think\Db\Where;
//use think\Exception;

class Qzuserrz extends Backend
{
    public function _initialize()
    {
        parent::_initialize();
    }

    //达人信息认证列表
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            $map['isdelete'] = ["eq", 0];
            $map['rz_state'] = ["neq", ""];
            $map['rz_type'] = ["eq", 1];
            //表单搜索
            if ($this->request->param('nickname')) {
                //昵称查询
                $id = Db::name('qz_user')->where('nickname', 'like', $this->request->param('nickname') . "%")
                    ->column('id');
                //dump($id);die;
                $map['id'] = ["in", $id];

//                if ($this->request->param('nickname')) {
//                    $map['nickname'] = ['nickname', "like", "%" . $this->request->param('nickname') . "%"];
//                }
            }
            //账号查询
            if ($this->request->param('userid')) {
                $map['userid'] = ['userid', "like", "%" . $this->request->param('userid') . "%"];
            }
            //手机号查询
            if ($this->request->param('phone')) {
                $map['phone'] = ['phone', "like", "%" . $this->request->param('phone') . "%"];
            }
            //性别查询
            if ($this->request->param('sex')) {
                $map['sex'] = ['eq', $this->request->param('sex')];
            }
            //身高查询
            if ($this->request->param('height')) {
                $height = explode('-', $this->request->param('height'));
                $map['height'] = ['between', [$height[0], $height[1]]];
            }
            //体重查询
            if ($this->request->param('weight')) {
                $weight = explode('-', $this->request->param('weight'));
                $map['weight'] = ['between', [$weight[0], $weight[1]]];
            }
            //区域查询
            if ($this->request->param('province')) {
                $map['province'] = ['eq', $this->request->param('province')];
            }
            //年龄查询
            if ($this->request->param('age')) {
                $age = explode('-', $this->request->param('age'));
                //dump($age);//ok
                //获取需要时间区间的时间戳
                $age['0'] = strtotime('-' . $age['0'] . " year");
                $age['1'] = strtotime('-' . $age['1'] . " year");
                $map['birthday'] = ['between', [$age['1'], $age['0']]];
            }

            if ($this->request->param('type') != "") {
                $map['type'] = ["eq", $this->request->param('type')];
            }

            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($map, $sort, $order, $offset, $limit) = $this->buildparams();

            //查询达人认证
            $list = Db::name('qz_user')->field('img,id,userid,nickname,province,city,area,sex,phone,use_type,height,
                                                positive_id_card_img,back_id_card_img,hold_id_card_img,rz_state,rz_time,birthday,
                                                weight,prased,card_num,creat_time')
                                            ->where($map)
                                            ->order($sort, $order)
                                            ->limit($offset, $limit)
                                            ->select();
            //获取js需要的数据
            $year = intval(date('Y'));
            foreach ($list as $k => $v) {
                $list[$k]['age'] = $year - (date('Y', $list[$k]['birthday']));
                $list[$k]['tige'] = $list[$k]['height'] . "/" . $list[$k]['weight'];
                $list[$k]['diqu'] = $list[$k]['province'] . "-" . $list[$k]['city'] . "-" . $list[$k]['area'];
            }
            $count = Db::name('qz_user')->where($map)->count();
            $result = array("total" => $count, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 达人信息认证编辑，审核
     * @param int $ids  用户的userid
     * @param str $edittype type==sh 进行审核
     * @param str $editstate 审核状态，2，通过，0，驳回
     */

    public function edit($ids = NULL){
        $param = $this->request->param();
        if(!is_null($ids)){
            if($this->request->isPost()){
                if(!empty($param)){
                    if(!empty($param['edittype']) && $param['edittype']=="sh"){
                        $res=Db::name('qz_user')->where('userid',$ids)->update(['rz_state'=>$param['editstate']]);
                        if($res){
                            $this->success('审核成功');
                        }else{
                            $this->success('审核失败');
                        }
                    }elseif(!empty($param['edittype']) && $param['edittype']=="edit"){
                        $data=$param;
                        $res=Db::name('qz_user')->where('userid',$ids)->save($data);
                        if($res){
                            $this->success('编辑成功');
                        }else{
                            $this->success('编辑失败');
                        }
                    }
                }
            }else{
                $row = Db::name('qz_user')->where('userid', $ids)
                    ->find();
                $this->assign('row', $row);
                return $this->view->fetch();
            }
        }
    }


    /**
     * 达人信息认证详情查看
     * @param int $ids  用户的userid
     */
    public function detail($ids = NULL){
        if(!is_null($ids)) {
            $row = Db::name('qz_user')->where('userid', $ids)->find();
            if (!$row)
                $this->error(__('失败'));
            if ($this->request->isAjax()) {
                $this->success("ok", null, ['id' => $ids]);
            }
            $this->view->assign("row", $row);
            return $this->view->fetch();
        }
    }
}